<template>
  <div class="meeting-file-list">
    <el-table :data="files" style="width: 100%">
      <el-table-column prop="name" label="文件名称" min-width="200" />
      <el-table-column prop="size" label="文件大小" width="120">
        <template #default="{ row }">
          {{ formatFileSize(row.size) }}
        </template>
      </el-table-column>
      <el-table-column prop="uploadTime" label="上传时间" width="180">
        <template #default="{ row }">
          {{ formatDate(row.uploadTime) }}
        </template>
      </el-table-column>
      <el-table-column prop="downloadCount" label="下载次数" width="100" />
      <el-table-column prop="lastDownload" label="最后下载" width="180">
        <template #default="{ row }">
          {{ row.lastDownload ? formatDate(row.lastDownload) : '未下载' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" fixed="right">
        <template #default="{ row }">
          <el-button
            type="primary"
            link
            :icon="Download"
            @click="handleDownload(row)"
          >
            下载
          </el-button>
          <el-button
            v-if="showDelete"
            type="danger"
            link
            :icon="Delete"
            @click="handleDelete(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup lang="ts">
import { Download, Delete } from '@element-plus/icons-vue'
import { MeetingFile } from '@/api/meeting'
import { ElMessage } from 'element-plus'
import { formatDate } from '@/utils/date'

const props = defineProps<{
  files: MeetingFile[]
  showDelete?: boolean
}>()

const emit = defineEmits<{
  (e: 'delete', file: MeetingFile): void
  (e: 'download', file: MeetingFile): void
}>()

const formatFileSize = (size: number) => {
  if (size < 1024) return size + 'B'
  if (size < 1024 * 1024) return (size / 1024).toFixed(2) + 'KB'
  if (size < 1024 * 1024 * 1024) return (size / 1024 / 1024).toFixed(2) + 'MB'
  return (size / 1024 / 1024 / 1024).toFixed(2) + 'GB'
}

const handleDownload = async (file: MeetingFile) => {
  // try {
  //   const response = await downloadMeetingFile(file.id)
  //   const blob = new Blob([response.data])
  //   const url = window.URL.createObjectURL(blob)
  //   const link = document.createElement('a')
  //   link.href = url
  //   link.download = file.name
  //   document.body.appendChild(link)
  //   link.click()
  //   document.body.removeChild(link)
  //   window.URL.revokeObjectURL(url)
  //   emit('download', file)
  //   ElMessage.success('文件下载成功')
  // } catch (error) {
  //   ElMessage.error('文件下载失败')
  // }
}

const handleDelete = (file: MeetingFile) => {
  emit('delete', file)
}
</script>

<style scoped>
.meeting-file-list {
  margin: 16px 0;
}
</style> 